package logic

import (
	"car_srv/models"
	"context"

	"car_srv/car"
	"car_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsListLogic {
	return &GoodsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品列表
func (l *GoodsListLogic) GoodsList(in *car.GoodsListRequest) (*car.GoodsListResponse, error) {
	var good models.Good
	good.Status = in.Status
	goodList, err := good.List()
	if err != nil {
		logx.Error("商品列表展示失败", err)
		return nil, err
	}

	var goodsList []*car.GoodsList
	for _, g := range goodList {
		list := car.GoodsList{
			Id:          int64(g.ID),
			GoodsTitle:  g.GoodsTitle,
			GoodsImage:  g.GoodsImage,
			GoodsInfo:   g.GoodsInfo,
			GoodsPrice:  float32(g.GoodsPrice),
			GoodsStock:  g.GoodsStock,
			GoodsStatus: g.GoodsStatus,
		}
		goodsList = append(goodsList, &list)
	}

	return &car.GoodsListResponse{
		List: goodsList,
	}, nil
}
